package com.gxa.servlet.activity;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.gxa.pojo.Result;
import com.gxa.pojo.activity.Activity;
import com.gxa.service.avticity.ActivityService;
import com.gxa.service.avticity.impl.ActivityServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet(urlPatterns = "/findAllActivity")
public class FindAllActivityServlet extends HttpServlet {

    ObjectMapper objectMapper = new ObjectMapper();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        this.doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=UTF-8");
        ActivityService activityService = new ActivityServiceImpl();


        List<Activity> activity = activityService.findAllActivity();
        Map<String,Object> map = new HashMap<>();
        String result = null;
        if (activity!=null){
            map.put("result", Result.ok("success",activity));
//            map.put("activity",activity);
            result= objectMapper.writeValueAsString(map);
        }else {
            map.put("result",Result.error());
            result= objectMapper.writeValueAsString(map);

        }

        PrintWriter writer = response.getWriter();
        writer.write(result);
        writer.flush();
        writer.close();


    }
}
